package cn.xuexiluxian.open.admin.controller.account;

import cn.hutool.core.util.StrUtil;
import cn.xuexiluxian.open.common.enums.ResultStatusCodeEnum;
import cn.xuexiluxian.open.common.enums.SmsCodeEnum;
import cn.xuexiluxian.open.common.response.ResponseResult;
import cn.xuexiluxian.open.common.utils.AesUtil;
import cn.xuexiluxian.open.system.model.dto.MobileCaptchaDTO;
import cn.xuexiluxian.open.system.model.entity.SystemUser;
import cn.xuexiluxian.open.system.service.ISystemUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController("userAccountController")
@RequestMapping("u")
public class AccountController {
    @Autowired
    private ISystemUserService systemUserService;
    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 找回密码
     * @return
     */
    @PostMapping("findPassword")
    public ResponseResult findPassword(@RequestBody MobileCaptchaDTO mobileCaptchaDTO){
        if(StrUtil.isBlank(mobileCaptchaDTO.getMobile())){
            return ResponseResult.error(ResultStatusCodeEnum.PARAM_REQUIRE_ERROR);
        }
        if(StrUtil.isBlank(mobileCaptchaDTO.getCaptcha())){
            return ResponseResult.error(ResultStatusCodeEnum.CAPTCHA_NOT_EMPTY);
        }

        String inputMobile = AesUtil.decrypt(mobileCaptchaDTO.getMobile());
        String inputCaptcha = AesUtil.decrypt(mobileCaptchaDTO.getCaptcha());

        String captcha = redisTemplate.opsForValue().get(SmsCodeEnum.FIND_PASSWORD.getKey() + mobileCaptchaDTO.getMobile());

        if(!inputCaptcha.equalsIgnoreCase(captcha)){
            return ResponseResult.error(ResultStatusCodeEnum.CAPTCHA_ERROR);
        }

        SystemUser member = systemUserService.selectOneByMobile(inputMobile);
        if(member == null){
            return ResponseResult.error(ResultStatusCodeEnum.USER_NOT_EXIST);
        }
        return ResponseResult.ok(member.getId());
    }

    /**
     * 重置密码
     * @return
     */
    @PostMapping("resetPassword")
    public ResponseResult resetPassword(@RequestBody MobileCaptchaDTO mobileCaptchaDTO){
        if(StrUtil.isBlank(mobileCaptchaDTO.getMobile())){
            return ResponseResult.error(ResultStatusCodeEnum.PARAM_REQUIRE_ERROR);
        }
        if(StrUtil.isBlank(mobileCaptchaDTO.getUserId())){
            return ResponseResult.error(ResultStatusCodeEnum.PARAM_REQUIRE_ERROR);
        }
        SystemUser newMember = new SystemUser();
        newMember.setId(mobileCaptchaDTO.getUserId());

        String mobile = AesUtil.decrypt(mobileCaptchaDTO.getMobile());
        //新密码
        String password = AesUtil.decrypt(mobileCaptchaDTO.getPassword());
        //重复新密码
        String repassword = AesUtil.decrypt(mobileCaptchaDTO.getRepassword());
        if(!password.equals(repassword)){
            return ResponseResult.error(ResultStatusCodeEnum.USER_PASSWORD_NOTMATCH);
        }

        SystemUser oldMember = systemUserService.selectOneByMobile(mobile);
        if(oldMember == null){
            return ResponseResult.error(ResultStatusCodeEnum.USER_NOT_EXIST);
        }

        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        password = passwordEncoder.encode(password);
        newMember.setPassword(password);
        boolean b = systemUserService.updateById(newMember);
        if (b) {
            return ResponseResult.ok();
        }
        return ResponseResult.error(ResultStatusCodeEnum.HTTP_ERROR);
    }
}
